﻿using System;

namespace LargestNumberLowerThanK
{
    class LargestNumberLowerThanK
    {
        static void Main(string[] args)
        {
            int searchedNumber, k, arrayLength;
            int[] numbers;

            Console.Write("Please enter the length of your array: ");
            arrayLength = int.Parse(Console.ReadLine());
            numbers = new int[arrayLength];
            for (int index = 0; index < arrayLength; index++)
            {
                Console.Write("Index [{0}]: ", index);
                numbers[index] = int.Parse(Console.ReadLine());
            }
            Console.Write("Enter the number K: ");
            k = int.Parse(Console.ReadLine());

            Array.Sort(numbers);
            searchedNumber = Array.BinarySearch(numbers, k);

            //Result
            foreach (int number in numbers)
            {
                Console.Write("{0} ", number);
            }
            Console.WriteLine();
            if (searchedNumber < 0)
            {
                if (searchedNumber == -1)
                {
                    Console.WriteLine("No such number");
                }
                else
                {
                    Console.WriteLine("Largest number lower than or equal to {0} is {1}", k, numbers[Math.Abs(searchedNumber) - 2]);
                }
            }
            else
            {
                Console.WriteLine("Largest number lower than or equal to {0} is {1}", k, numbers[searchedNumber]);
            }
        }
    }
}
